<?php

namespace App\Store\Controllers\Financial\Goods;

use App\Common\Services\CommonExcelExport;
use App\Common\Services\Store\Common as StoreCommon;
use App\Http\Controllers\Controller;
use App\Store\Models\Goods\GoodsStockList;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class GoodsTimeController extends Controller
{
    public function getlist(Request $request)
    {
        $list = $this->getdata($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    public function doexport(Request $request)
    {
        $list = $this->getdata($request);

        if (!$list['list']) {
            return response()->json(['code' => '001', 'msg' => '导出失败']);
        }
        $storeNames = array_unique(array_map(function($item) {
            return $item['storeinfo']['store_name'];
        }, $list['list']));
        $row = [
            ['store_name'=>'库龄分析报表'],
            ['store_name'=>'所属门店：'.join(',' ,$storeNames)],
            [
            'store_name' => '所属门店',
            'goods_category_id' => '类别',
            'goods_style_id' => '款式',
            '0_6_count' => '0-6月-件数',
            '0_6_money' => '0-6月-金额',
            '7_12_count' => '7-12月-件数',
            '7_12_money' => '7-12月-金额',
            '12_24_count' => '12-24月-件数',
            '12_24_money' => '12-24月-金额',
            '24_36_count' => '24-36月-件数',
            '24_36_money' => '24-36月-金额',
            '36_count' => '36月以上-件数',
            '36_money' => '36月以上-金额',
            'total_count' => '总计件数',
            'total_money' => '总计金额',
        ]];

        $is_store_account = StoreCommon::getIsStoreAccount($request);
        if (!$is_store_account) {
            unset($row[0]['0_6_money']);
            unset($row[0]['7_12_money']);
            unset($row[0]['12_24_money']);
            unset($row[0]['24_36_money']);
            unset($row[0]['36_money']);
            unset($row[0]['total_money']);
        }

        $exceldata = [];
        foreach ($list['list'] as $val) {
            $exceldata[] = [
                'store_name' => $val['storeinfo']['store_name'],
                'goods_category_id' => $val['goods_category']['category_name'],
                'goods_style_id' => $val['goods_style']['style_name'],
                '0_6_count' => $val['0_6']['count'],
                '0_6_money' => $val['0_6']['money'] / 100,
                '7_12_count' => $val['7_12']['count'],
                '7_12_money' => $val['7_12']['money'] / 100,
                '12_24_count' => $val['12_24']['count'],
                '12_24_money' => $val['12_24']['money'] / 100,
                '24_36_count' => $val['24_36']['count'],
                '24_36_money' => $val['24_36']['money'] / 100,
                '36_count' => $val['36']['count'],
                '36_money' => $val['36']['money'] / 100,
                'total_count' => $val['total']['count'],
                'total_money' => $val['total']['money'] / 100,
            ];
        }

        $filename = '库龄分析报表导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }

    public function getdata(Request $request)
    {
        $list = GoodsStockList::gettimelist($request);

        if ($list['list']) {
            foreach ($list['list'] as $k => $v) {
                // 0-6
                $start_time = time() - 6 * 30 * 86400;
                $end_time = time();
                $res1 = GoodsStockList::gettimetotal($v, $start_time, $end_time);
                if ($res1) {
                    $list['list'][$k]['0_6']['count'] = $res1['count'];
                    $list['list'][$k]['0_6']['money'] = $res1['money'];
                } else {
                    $list['list'][$k]['0_6']['count'] = '0';
                    $list['list'][$k]['0_6']['money'] = '0';
                }

                // 7-12
                $start_time = time() - 12 * 30 * 86400;
                $end_time = time() - 7 * 30 * 86400;
                $res2 = GoodsStockList::gettimetotal($v, $start_time, $end_time);
                if ($res2) {
                    $list['list'][$k]['7_12']['count'] = $res2['count'];
                    $list['list'][$k]['7_12']['money'] = $res2['money'];
                } else {
                    $list['list'][$k]['7_12']['count'] = '0';
                    $list['list'][$k]['7_12']['money'] = '0';
                }

                // 12-24
                $start_time = time() - 24 * 30 * 86400;
                $end_time = time() - 12 * 30 * 86400;
                $res3 = GoodsStockList::gettimetotal($v, $start_time, $end_time);
                if ($res3) {
                    $list['list'][$k]['12_24']['count'] = $res3['count'];
                    $list['list'][$k]['12_24']['money'] = $res3['money'];
                } else {
                    $list['list'][$k]['12_24']['count'] = '0';
                    $list['list'][$k]['12_24']['money'] = '0';
                }

                // 24-36
                $start_time = time() - 36 * 30 * 86400;
                $end_time = time() - 24 * 30 * 86400;
                $res4 = GoodsStockList::gettimetotal($v, $start_time, $end_time);
                if ($res4) {
                    $list['list'][$k]['24_36']['count'] = $res4['count'];
                    $list['list'][$k]['24_36']['money'] = $res4['money'];
                } else {
                    $list['list'][$k]['24_36']['count'] = '0';
                    $list['list'][$k]['24_36']['money'] = '0';
                }

                // 36
                $start_time = 0;
                $end_time = time() - 36 * 30 * 86400;
                $res5 = GoodsStockList::gettimetotal($v, $start_time, $end_time);
                if ($res5) {
                    $list['list'][$k]['36']['count'] = $res5['count'];
                    $list['list'][$k]['36']['money'] = $res5['money'];
                } else {
                    $list['list'][$k]['36']['count'] = '0';
                    $list['list'][$k]['36']['money'] = '0';
                }

                $res5 = GoodsStockList::gettimetotal($v, 0, 0);
                if ($res5) {
                    $list['list'][$k]['total']['count'] = $res5['count'];
                    $list['list'][$k]['total']['money'] = $res5['money'];
                } else {
                    $list['list'][$k]['total']['count'] = '0';
                    $list['list'][$k]['total']['money'] = '0';
                }
            }
        }
        return $list;
    }
}
